package com.mjfeed.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.mjfeed.adapter.OrderAdapter;
import com.mjfeed.bean.OrderJSON;
import com.mjfeed.bean.OrderListJSON;
import com.mjfeed.service.OrderService;
import com.mjfeed.swipeback.SwipeBackActivity;
import com.mjfeed.util.DateUtils;
import com.mjfeed.util.NetUtils;
import com.mjfeed.util.SPUtils;
import com.mjfeed.widget.LoadingDialog;
import com.mjfeed.widget.XListView;
import com.mjfeed.widget.XListView.IXListViewListener;

/**
 * 我的订单
 * @author zhaominglei
 * @date 2014-12-16
 * 
 */
public class OrderListActivity extends SwipeBackActivity implements OnClickListener, IXListViewListener {
	@SuppressWarnings("unused")
	private static final String TAG = OrderListActivity.class.getSimpleName();
	
	private ImageView goHome;
	private LoadingDialog loadingDialog; //加载框
	private XListView orderListView;
	private List<OrderJSON> orders = new ArrayList<OrderJSON>();
	private OrderAdapter orderAdapter;
	private int page = 1; //页码
	private String referer; //来自那个链接
	private String keyword; //关键词
	private String username; //用户名
	private String sign; //签名
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_list);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.order_list_gohome);
		orderListView = (XListView) findViewById(R.id.order_list_listview);
		orderAdapter = new OrderAdapter(this, orders);
		orderListView.setPullLoadEnable(true);
		orderListView.setXListViewListener(this);
		orderListView.setAdapter(orderAdapter);
		
		goHome.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		orderListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				TextView productId = (TextView)view.findViewById(R.id.product_list_template_id);
//				Intent intent = new Intent(view.getContext(), ProductDetailActivity.class);
//				intent.putExtra("productId", Long.valueOf(productId.getText().toString()));
//				intent.putExtra("keyword", keyword);
//				intent.putExtra("referer", "ProductListActivity");
//				startActivity(intent);
			}
		});
		
		referer = getIntent().getStringExtra("referer");
		keyword = getIntent().getStringExtra("keyword");
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		getOrders();
	}

	private void getOrders() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new OrderListAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
			onLoad();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.order_list_gohome:
			finish();
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onRefresh() {
		getOrders();
	}

	@Override
	public void onLoadMore() {
		getOrders();
		page++;
	}
	
	// 刷新结束
	public void onLoad() {
		orderListView.stopRefresh();
		orderListView.stopLoadMore();
		orderListView.setRefreshTime(DateUtils.format(new Date(), DateUtils.datePattern));
	}
	
	public class OrderListAsyncTask extends AsyncTask<String, String, OrderListJSON> {
		
		@Override
		protected OrderListJSON doInBackground(String... params) {
			return new OrderService().getOrder(referer, keyword, page, username, sign);
		}

		@Override
		protected void onPostExecute(OrderListJSON result) {
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				orderAdapter.setOrders(result.getRecordList());
				orderAdapter.notifyDataSetChanged();
				onLoad();
			}
			loadingDialog.dismiss();
			if (result != null && !result.isHasNextPage()) {
				Toast.makeText(getApplicationContext(), R.string.load_more_finish,	Toast.LENGTH_SHORT).show();
				orderListView.setPullLoadEnable(false);
			} 
		}
	}
}
